﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.DynamicData.ModelProviders;
using System.Collections.ObjectModel;
using NHibernate.Mapping;

namespace DD.NHibernateProvider
{
    public class NHibernateTableProvider : TableProvider
    {
        private List<ColumnProvider> columns = new List<ColumnProvider>();

        public NHibernateTableProvider(NHibernateDataModelProvider dataModel, PersistentClass classMapping)
            : base(dataModel)
        {
            this.Name = classMapping.Table.Name;
            this.EntityType = classMapping.MappedClass;
            foreach (var column in classMapping.Table.ColumnIterator)
            {
                var col = new NHibernateColumnProvider(this, column, false);
                this.columns.Add(col);
            }

            foreach (var column in classMapping.Table.PrimaryKey.ColumnIterator)
            {
                var col = new NHibernateColumnProvider(this, column, true);
                this.columns.Add(col);
            }
        }

        public override ReadOnlyCollection<ColumnProvider> Columns
        {
            get { return columns.AsReadOnly(); }
        }

        public override IQueryable GetQuery(object context)
        {
            throw new NotImplementedException();
        }
    }
}
